package com.supplychainapi.common;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Map;

/**
 * Created by wangziqi on 2017/9/20.
 */
@ApiModel("JSON 响应")
public class RestResponse<T> {
    @ApiModelProperty(
            value = "标志码，200 表示完全成功",
            example = "200"
    )
    private int code;
    @ApiModelProperty("正经数据")
    private T data;
    @ApiModelProperty("额外数据")
    private Map<String, Object> extra = Maps.newHashMap();
    @ApiModelProperty("消息提示")
    private String message;
    @ApiModelProperty("细分异常代码")
    private int errorCode;
    @ApiModelProperty("时间戳")
    private long time = System.currentTimeMillis();

    @ApiModelProperty("分页信息")
    private PageInfo<T> pageInfo;

    public static final <TT> RestResponse<TT> success() {
        return new RestResponse(200, null, "success");
    }

    public static final <TT> RestResponse<TT> success(TT data) {
        return new RestResponse(200, data, "success");
    }

    public static final <TT> RestResponse<TT> success(TT data,PageInfo<TT> pageInfo) {
        return new RestResponse(200, data, "success", pageInfo);
    }

    public static final <TT> RestResponse<TT> success(TT data, String message) {
        return new RestResponse(200, data, message);
    }

    public static final <TT> RestResponse<TT> success(TT data, Map<String, Object> extra) {
        return new RestResponse(200, data, "success", extra);
    }

    public static final <TT> RestResponse<TT> success(TT data, String message, Map<String, Object> extra) {
        return new RestResponse(200, data, message, extra);
    }

    public static final <TT> RestResponse<TT> fail(String message) {
        return new RestResponse(500, null, message);
    }

    public static final <TT> RestResponse<TT> fail(Exception e) {
        return new RestResponse(500, -1, e.getCause() == null ? e.getMessage() : e.getCause().getLocalizedMessage());
    }

    public static final <TT> RestResponse<TT> fail(int code, String message) {
        return new RestResponse(code, -1, message);
    }

    public static final <TT> RestResponse<TT> fail(int code, int errorCode, String message) {
        return new RestResponse(code, errorCode, message);
    }

    private RestResponse(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    private RestResponse(int code, T data, String message,PageInfo<T> pageInfo) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.pageInfo=pageInfo;
    }

    private RestResponse(int code, T data, String message, Map<String, Object> extra) {
        this.code = code;
        this.data = data;
        this.message = message;
        if(extra != null) {
            this.extra.putAll(extra);
        }

    }



    private RestResponse(int code, int errorCode, String message) {
        this.code = code;
        this.errorCode = errorCode;
        this.message = message;
    }

    private RestResponse(int code, int errorCode, String message, Map<String, Object> extra) {
        this.code = code;
        this.errorCode = errorCode;
        this.message = message;
        if(extra != null) {
            this.extra.putAll(extra);
        }

    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getErrorCode() {
        return this.errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public long getTime() {
        return this.time;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getExtra() {
        return this.extra;
    }

    public <T> Object getExtra(String name) {
        return this.extra.get(name);
    }

    public void addExtra(String name, Object value) {
        this.extra.put(name, value);
    }

    public PageInfo getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }
}
